package com.hxgz.steel.service.Impl;

import com.google.common.collect.Maps;
import com.hxgz.steel.common.ServerResponse;
import com.hxgz.steel.dao.*;
import com.hxgz.steel.domain.*;
import com.hxgz.steel.dto.CategoryTimeDto;
import com.hxgz.steel.service.WebService;
import com.hxgz.steel.util.OSSClientUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
public class WebServiceImpl implements WebService {

  @Autowired
  AdManagerMapper adManagerMapper;

  @Autowired
  AnnouncementManagerMapper announcementManagerMapper;

  @Autowired
  SocerManagerMapper socerManagerMapper;

  @Autowired
  CategoryMapper categoryMapper;

  @Autowired
  AgreementManagerMapper agreementManagerMapper;

  @Autowired
  ConsumerNewsManagerMapper consumerNewsManagerMapper;

  @Autowired
  NewsManagerMapper newsManagerMapper;

  @Autowired
  public OSSClientUtil ossutil;
  @Autowired
  private NavigationManagerMapper navigationManagerMapper;


  @Override
  public ServerResponse getAdManager(int status) {
    List<AdManager> list = null;
    if (status == 0) {
      list = adManagerMapper.getAdList(status);
    } else {
      list = adManagerMapper.getBannerList(status);
    }
    return ServerResponse.createBySuccess(list);
  }

  @Override
  @Transactional
  public ServerResponse updateAdManager(String id, MultipartFile multipartFile,
      HttpServletRequest request) {
    AdManager adManager = adManagerMapper.selectByPrimaryKey(Long.valueOf(id));
   /* UploadPic pic = new UploadPic();
    String filePath = pic.upload(multipartFile);
    File file = new File(filePath);
    String fileName = file.getName();
    String pealPath = PropertiesUtil.getProperty("file.server.peal") + fileName;*/
    String imgurl = "";
    try {
      String name = ossutil.uploadImg2Oss(multipartFile);
      imgurl = ossutil.getImgUrl(name);
    } catch (Exception e) {
      e.printStackTrace();
    }
    adManager.setPic(imgurl);
    int i = adManagerMapper.updateByPrimaryKeySelective(adManager);
    if (i > 0) {
      return ServerResponse.createBySuccessMessage(imgurl);
    }
    return ServerResponse.createByErrorMessage("更新失败");
  }

  @Override
  public ServerResponse checkAn() {
    List<AnnouncementManager> list = announcementManagerMapper.getAllList();
    return ServerResponse.createBySuccess(list);
  }

  @Override
  @Transactional
  public ServerResponse addAn(String content) {
    AnnouncementManager announcementManager = new AnnouncementManager();
    announcementManager.setContent(content);
    int i = announcementManagerMapper.insertSelective(announcementManager);
    return i == 1 ? ServerResponse.createBySuccessMessage("增加成功") : ServerResponse.createByError();
  }

  @Override
  @Transactional
  public ServerResponse delete(Long aLong) {
    int i = announcementManagerMapper.deleteByPrimaryKey(aLong);
    return i == 1 ? ServerResponse.createBySuccessMessage("删除成功") : ServerResponse.createByError();
  }

  @Override
  public ServerResponse checkCategory(Integer parentId,Integer status) {
    List<CategoryTimeDto> listByFuncId = categoryMapper.getListByParentId(parentId,status);
    for (CategoryTimeDto categoryTimeDto : listByFuncId) {
      HashMap<Object, Object> map = Maps.newHashMap();
      map.put("funcId", 3);
      map.put("id", categoryTimeDto.getId());
      map.put("status",status);
      List<Category> listCa = categoryMapper.getListByFuncIdAndId(map);
      categoryTimeDto.setList(listCa);
    }
    return ServerResponse.createBySuccess(listByFuncId);
  }

  @Override
  @Transactional
  public ServerResponse updateCategory(String id, String name) {
    Category category = categoryMapper.selectByPrimaryKey(Integer.valueOf(id));
    if (StringUtils.isEmpty(name)) {
      Integer status = category.getStatus();
      if (status == 1) {
        category.setStatus(2);
      } else {
        category.setStatus(1);
      }
    } else {
      category.setName(name);
    }
    int i = categoryMapper.updateByPrimaryKeySelective(category);
    return i == 1 ? ServerResponse.createBySuccessMessage("修改成功")
        : ServerResponse.createByError();
  }

  @Override
  @Transactional
  public ServerResponse addCategory(String name, String id, String funcId) {
    Category category = new Category();
    category.setName(name);
    category.setParentId(Integer.valueOf(id));
    category.setFuncId(Integer.valueOf(funcId) + 1);
    category.setStatus(1);
    int i = categoryMapper.insertSelective(category);
    return i == 1 ? ServerResponse.createBySuccessMessage("添加成功") : ServerResponse.createByError();
  }

  @Override
  public ServerResponse checkSocerManager() {
    List<SocerManager> allList = socerManagerMapper.getAllList();
    return ServerResponse.createBySuccess(allList);
  }

  @Override
  @Transactional
  public ServerResponse updateSocerManager(String id, String socer) {
    SocerManager socerManager = socerManagerMapper.selectByPrimaryKey(Long.valueOf(id));
    if (socer != null) {
      socerManager.setSocer(socer);
    } else {
      if (socerManager.getStatus() == 1) {
        socerManager.setStatus(2);
      } else {
        socerManager.setStatus(1);
      }
    }
    int i = socerManagerMapper.updateByPrimaryKeySelective(socerManager);
    return i == 1 ? ServerResponse.createBySuccessMessage("修改成功") : ServerResponse.createByError();
  }

  @Override
  @Transactional
  public ServerResponse updateaAreementManager(String content) {
    AgreementManager agreementManager = agreementManagerMapper.selectByPrimaryKey((long) 1);
    agreementManager.setContent(content);
    int i = agreementManagerMapper.updateByPrimaryKeySelective(agreementManager);
    NavigationManager navigationManager = navigationManagerMapper.selectByPrimaryKey((long)34);
    navigationManager.setContent(content);
    int j = navigationManagerMapper.updateByPrimaryKeySelective(navigationManager);
    return (i == 1 && j==1) ? ServerResponse.createBySuccessMessage("修改成功") : ServerResponse.createByError();
  }

  @Override
  public ServerResponse checkConsumerNews() {
    ConsumerNewsManager consumerNewsManager = consumerNewsManagerMapper
        .selectByPrimaryKey((long) 1);
    return ServerResponse.createBySuccess(consumerNewsManager);

  }

  @Override
  @Transactional
  public ServerResponse updateConsumerNews() {
    ConsumerNewsManager consumerNewsManager = consumerNewsManagerMapper
        .selectByPrimaryKey((long) 1);
    if(consumerNewsManager==null){
      ConsumerNewsManager consumerNewsManager1 = new ConsumerNewsManager();
      consumerNewsManager1.setId(Long.valueOf(1));
      consumerNewsManager1.setStatus(1);
      consumerNewsManager1.setCreateTime(new Date());
      consumerNewsManagerMapper.insertSelective(consumerNewsManager1);
      return ServerResponse.createBySuccessMessage("修改成功");
    }
    if (consumerNewsManager.getStatus() == 1) {
      consumerNewsManager.setStatus(2);
    } else {
      consumerNewsManager.setStatus(1);
    }
    int i = consumerNewsManagerMapper.updateByPrimaryKeySelective(consumerNewsManager);
    return i == 1 ? ServerResponse.createBySuccessMessage("修改成功") : ServerResponse.createByError();
  }

  @Override
  public ServerResponse checkNewsManager(Integer belongId) {
    List<NewsManager> list = newsManagerMapper.selectByBelongId(belongId);
    return ServerResponse.createBySuccess(list);
  }

  @Override
  @Transactional
  public ServerResponse addNewsManager(Integer belongId, String title, String content) {
    NewsManager newsManager = new NewsManager();
    newsManager.setBelongid(belongId);
    newsManager.setTitle(title);
    newsManager.setContent(content);
    int i = newsManagerMapper.insertSelective(newsManager);
    return i == 1 ? ServerResponse.createBySuccessMessage("添加成功") : ServerResponse.createByError();
  }

  @Override
  @Transactional
  public ServerResponse deleteNewsManager(String id) {
    int i = newsManagerMapper.deleteByPrimaryKey(Long.valueOf(id));
    return i == 1 ? ServerResponse.createBySuccessMessage("删除成功") : ServerResponse.createByError();
  }

  @Override
  public ServerResponse checkNewsManagerId(String id) {
    NewsManager newsManager = newsManagerMapper.selectByPrimaryKey(Long.valueOf(id));
    return ServerResponse.createBySuccess(newsManager);
  }

  @Override
  public ServerResponse checkgetAgreementManager() {
    AgreementManager agreementManager = agreementManagerMapper.selectByPrimaryKey(Long.valueOf(1));
    return ServerResponse.createBySuccess(agreementManager);
  }


}
